<?php
 require "../../document/class_corpus.php";
 require "../../document/class_document.php";
 require "../../document/class_document_xml.php";

 require "../../files/tool_files_dirs.php";
 require "../../misc/class_tag.php";

 require "../../tree/class_tree.php";
 require "../../tree/class_tree_xhtml.php";
 require "../../tree/class_structure.php";
 require "../../tree/tool_tree.php";

 require "../../array/tool_array.php";

 require "../../string/tool_string.php";
 require "../../string/class_segment.php";

 require "../../pdo/class_sql_document.php";

$exp=new sql_experience("align_ecr_selected");
$array_obj_sql=$exp->reader_experience();
$array_data=array();

$data_lg=$array_obj_sql['sql_document']->reader_all_lg();

$corpus_multidoc = array();

//$data_lg = array( array("lg"=>"fr") );
$id_chunk = 1;

$array_factory = array();
foreach($data_lg as $lg){
  $array_factory[$lg['lg']] = new factory_mot();
  $r = $array_obj_sql['sql_rstr']->reader_all2relative_id_doc($lg["lg"]);
  $k = key($r);
  foreach($r[$k] as $tid=>$id){
    $corpus_multidoc[$id][$k] = $tid;
  }
}

//$id_multidoc = 1;
//$multidoc = $corpus_multidoc[$id_multidoc];

$doc2bloc = array();
$id_bloc2text = array();

foreach($corpus_multidoc as $id_multidoc=>$multidoc){
  echo "id_multidoc :: $id_multidoc\n";
  foreach($multidoc as $lg=>$id_doc){
  $r = $array_obj_sql['sql_bloc']->reader_id_document2array($id_doc);
  foreach($r as $info_bloc){
    $id_bloc = $info_bloc['id_bloc'];
    $id_bloc2text[$id_bloc] = $info_bloc['contenu_text'];
    $array_word = $array_obj_sql['sql_word']->reader_id_bloc2info_word($id_bloc);

    foreach($array_word as $word){
      $mot = new mot($id_bloc2text[$id_bloc],
                     $word['offset'],$word['len'],
                     array("id_bloc"=>$id_bloc,"id_doc"=>$lg,"id_word"=>$word['id_word']));
      $array_factory[$lg]->__add_word($mot,$lg,$id_bloc);
    }
  }
  $doc2bloc[$lg] = $r;
  }
}

$array_lg_cut = array('fi','hu','et','pl','cs','lt','lv','sl');

foreach($array_factory as $lg=>$factory){
  $array_data = array();
  print "$lg::e\n";
  $factory->init_etiquetage();
  print "$lg::c\n";
  if(in_array($lg,$array_lg_cut)){
//    echo "  !!\n";
    $factory->init_chunk_with_cut();
  }
  else{
//    echo "  ++\n";
    $factory->init_chunk();
  }
  print "$lg::get\n";
  $rep = $factory->__get_repartition_chunk();
  print "$lg::svg\n";
  foreach($rep as $id_doc=>$array_doc){
    foreach($array_doc as $id_bloc=>$array_bloc){
      foreach($array_bloc as $chunk){
        foreach($chunk as $id_word){
          $word = $factory->__get_segment($id_word);
          $id_word = $word->__get_id_word();
          $type = $word->__get_type();
          $array_data[]=array(
            'id_chunk'=>$id_chunk,
            'id_word'=>$id_word,
            'type'=>$type,
          );
        }
        ++$id_chunk;
      }
    }
  }
  $array_obj_sql['sql_chunk']->writer_array_values($array_data);
  unset($array_data);
}


die();



?>
